/*
 *  Copyright 2008,2009 Matthieu Chase Heimer
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.macro10.xroads.service;

import com.macro10.switchboard.service.UserTransaction;
import com.macro10.xml.SwitchboardParser;
import com.macro10.xroads.entity.Area;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class AreaService {

    private static final Logger logger = Logger.getLogger(AreaService.class.getPackage().getName());
    protected UserTransaction ut;
    protected EntityManager entityManager;
    protected EntityTransaction entityTransaction;

    public AreaService(UserTransaction ut) {
        this.ut = ut;
        entityManager = ut.getEntityManager();
        entityTransaction = ut.getEntityTransaction();
    }

    public boolean doesAreaExist(String areaName) {
        if (SwitchboardParser.getInstance().getXMLArea(areaName) != null) {
            return true;
        } else {
            return false;
        }
    }

    public Area getArea(String areaName) throws Exception {
        Query query = entityManager.createNamedQuery("findAreaByName");
        query.setParameter("areaName", areaName);
        try {
            Area area = (Area) query.getSingleResult();
            return area;
        } catch (NoResultException nre) {
            Area area = new Area(areaName);
            entityManager.persist(area);
            return area;
        } catch (Exception e) {
            entityTransaction.setRollbackOnly();
            logger.log(Level.ERROR, "Exception in getArea(" + areaName + ")", e);
            throw new Exception("Exception in getArea(" + areaName + ")", e);
        }
    }

    public boolean deleteArea(String areaName) throws Exception {
        Query query = entityManager.createNamedQuery("findAreaByName");
        query.setParameter("areaName", areaName);
        try {
            Area area = (Area) query.getSingleResult();
            entityManager.remove(area);
            //entityManager.flush();
            return true;
        } catch (NoResultException nre) {
            return false;
        } catch (Exception e) {
            entityTransaction.setRollbackOnly();
            logger.log(Level.ERROR, "Exception in deleteArea(" + areaName + ")", e);
            throw new Exception("Exception in deleteArea(" + areaName + ")", e);
        }

    }
}
